class InfoController < ApplicationController
  caches_page :index,:method_missing
  before_filter :user_authorize, :except =>[:index,:method_missing]
  verify :method => :post,
  :only =>[:save_picture,:update_user,:update_profile,:update_tag,:delete_incumbent,:delete_company,:update_incumbent,:update_company,:update_password], 
  :redirect_to => {:controller=>"info",:action => "info"}
  
  def method_missing(name, *args) 
    render :template=>"index/null"
  end
  
  def index
    redirect_to :action=>"info"
  end
  
  
  def info 
    @user = User.find_by_id(@activeuser.user_id)
    @incumbents = @user.incumbents
    @friends = @user.friends
    @companies = @user.companies
    @inbox_messages = @user.inbox_messages
    @outbox_messages = @user.outbox_messages
    @reports = @user.reports
  end
  
  def upload_picture
    # object=0 user
    @id=params[:id]
    @cat_id=params[:cat_id]
    @obj_id=params[:obj_id]
    render :layout=>false
  end
  
  def save_picture
    obj_id=params[:obj_id].to_i
    cat_id=params[:cat_id].to_i
    if request.post?
      if picture=@activeuser.create_picture(params[:picture],cat_id,obj_id)
        @activeuser.delete_picture(params[:id])
        flash[:notice]="图片上传成功"
        redirect_to :controller=>"info",:action=>"upload_picture",:id=>picture.id, :obj_id=>obj_id, :cat_id=>cat_id
      else
        flash[:notice]="图片格式不符，上传失败"
        redirect_to :controller=>"info",:action=>"upload_picture",:id=>id, :obj_id=>obj_id, :cat_id=>cat_id
      end 
    end
  rescue  
    flash[:notice]="图片格式不符，上传失败"
    redirect_to :controller=>"info",:action=>"upload_picture",:id=>id, :obj_id=>obj_id, :cat_id=>cat_id
  end
  
  
  def hide_user
    render :inline =>%{<div align="right"><strong><%= link_to_remote "+修改用户资料>>", :update=>"modify_user", :url=>{:controller=>"info", :action=>"modify_user"} %></strong></div>}
  end
  
  def modify_user
    @user = User.find_by_id(@activeuser.user_id)
    render :partial => "modify_user", :locals => {:user => @user}
  end
  
  def update_user
    if @error=@activeuser.update_user(params[:user])
      @height=@error.length*15+40
      render :update do |page|
        page.replace_html :check,"&nbsp;<script type='text/javascript'>openAlert('#{@error}',200,#{@height});</script>"
        page.replace_html :modify_user,'<div align="right"><strong>'+link_to_remote("+修改用户资料>>", :update=>"modify_user", :url=>{:controller=>"info", :action=>"modify_user"})+'</strong></div>'
      end
    else
      render :update do |page|
        page.replace_html :user_data, :partial=>"user_info",:locals => {:user => @activeuser.user}
      end
    end
  end
  
  def hide_profile
    render :inline =>%{<div align="right"><strong><%= link_to_remote "+修改个人信息>>", :update=>"modify_profile", :url=>{:controller=>"info", :action=>"modify_profile"} %></strong></div>}
  end
  
  def modify_profile
    @profile = User.find_by_id(@activeuser.user_id).profile
    @profile.texperience=@profile.texperience.to_i
    render :partial => "modify_profile", :locals => {:profile => @profile}
  end
  
  def update_profile
    if @error=@activeuser.update_profile(params[:profile])
      @height=@error.length*15+40
      render :update do |page|
        page.replace_html :check,"&nbsp;<script type='text/javascript'>openAlert('#{@error}',200,#{@height});</script>"
        page.replace_html :modify_profile,'<div align="right"><strong>'+link_to_remote("+修改个人信息>>", :update=>"modify_profile", :url=>{:controller=>"info", :action=>"modify_profile"})+'</strong></div>'
      end
    else
      render :update do |page|
        page.replace_html :profile_data, :partial=>"profile_info",:locals => {:profile => @activeuser.user.profile}
      end
    end
  end
  
  def modify_tag
    object_id = params[:obj_id].to_i
    @category_id = params[:category_id].to_i
    @obj=Category.find_object(object_id,@category_id)
    render :layout=>false
  end
  
  def update_tag
    object_id = params[:obj_id].to_i
    search_tag = params[:obj][:search_tag]
    @category_id = params[:category_id].to_i
    @obj=Category.find_object(object_id,@category_id)
    flash[:notice]=@activeuser.update_tag(@obj,search_tag)
    redirect_to :controller=>"info",:action=>"modify_tag",:obj_id=>@obj.id,:category_id=>@category_id
  end
  
  def delete_incumbent
    incumbent_id = params[:id] || -1
    if @activeuser.delete_incumbent(incumbent_id)
      render :inline=>"<script type='text/javascript'>new Effect.Puff('incumbent"+incumbent_id+"');</script>"
    else
      render :inline=>%{<script type="text/javascript">openAlert('对不起，您不能删除这条样本，谢谢!',200,50);</script>}
    end
  end
  
  def modify_incumbent
    incumbent_id = params[:id] || -1
    @incumbent=Incumbent.find_by_id(incumbent_id)
    @incumbent.teffective_year=@incumbent.teffective_year.to_i
    company=@incumbent.view_company
    render  :update do |page|
      page.replace_html "incumbent"+incumbent_id,:partial=>"report/modify_incumbent",:locals => {:incumbent => @incumbent,:company=> company}
    end
  end
  
  def hide_incumbent
    incumbent_id = params[:id] || -1
    incumbent=Incumbent.find_by_id(incumbent_id)
    render  :update do |page|
      page.replace_html "incumbent"+incumbent_id,:partial=>"report/incumbent_list_info",:locals => {:incumbent => incumbent}
    end
  end
  
  def update_incumbent
    
    if @error=@activeuser.update_incumbent(params[:incumbent])
      @height=@error.length*15+40
      render :update do |page|
        page.replace_html :check,"&nbsp;<script type='text/javascript'>openAlert('#{@error}',200,#{@height});</script>"
        page.replace_html :modify_profile,'<div align="right"><strong>'+link_to_remote("+修改个人信息>>", :update=>"modify_profile", :url=>{:controller=>"info", :action=>"modify_profile"})+'</strong></div>'
      end
    else
      render :update do |page|
        page.replace_html :profile_data, :partial=>"profile_info",:locals => {:profile => @activeuser.user.profile}
      end
    end
  end
  
  
  def delete_company
    company_id = params[:id] || -1
    if @activeuser.delete_company(company_id)
      render :inline=>"<script type='text/javascript'>new Effect.Puff('company"+company_id+"');</script>"
    else
      render :inline=>%{<script type="text/javascript">openAlert('对不起，由于公司您不能删除这条样本，谢谢!',200,50);</script>}
    end
  end
  
  def modify_company
    company_id = params[:id] || -1
    @company=Company.find_by_id(company_id)
    render  :update do |page|
      page.replace_html "company"+company_id,:partial=>"report/modify_company",:locals => {:company=> @company}
    end
  end
  
  def hide_company
    company_id = params[:id] || -1
    company=Company.find_by_id(company_id)
    render  :update do |page|
      page.replace_html "company"+company_id,:partial=>"report/company_list_info",:locals => {:company=> company}
    end
  end
  
  def update_company
  end
  
  def modify_password
    render :layout=>false
  end
  
  def update_password
    flash[:notice]=@activeuser.update_password(params[:user][:old_password],params[:user][:new_password],params[:user][:confirm_password])
    redirect_to :controller=>"info",:action=>"modify_password"
  rescue
    flash[:notice]="对不起，你无权修改密码"
    redirect_to :controller=>"info",:action=>"modify_password"
  end
  
  def show_message
    mgser_id = params[:id] || -1
    if message=@activeuser.create_outbox_message(mgser_id)
      render :partial => "create_message", :locals => {:object => message}
    else
      redirect_to :controller=>'login',:action=> 'login'
    end
  end
  
  def hide_message
    render(:nothing => true)
  end
  
  
  def create_message
    mgser_id = params[:id] || -1
    @message=@activeuser.create_message(mgser_id,params[:message])
    headers["Content-Type"] = "text/javascript; charset=UTF-8"
  end
  
  def read_user
    id = params[:id] || -1  
    if @user = User.find_by_id_and_usergroup_id(id,1)
      if @user==@activeuser.user
        redirect_to :controller=>"info"
      end
    end
  end
  
  def read_message
    id = params[:id] || -1
    owner_id = session[:user_id] || -1
    @message = Message.find_by_id_and_owner_id(id,owner_id)
  rescue
    redirect_to :controller => "index"
  end
  
  def create_friend
    friend_id = params[:id] || -1
    if friend=@activeuser.create_friend(friend_id)
      @message="您已经将"+friend.name+"加为了您的好友了"
      render :update do |page|
        page.replace_html :message,"&nbsp;<script type='text/javascript'>openAlert('#{@message}',200,60);</script>"
        page.redirect_to :controller=>'info'
      end
      friend.save
    else
      @message="您无法将他/她加为您的好友了"
      render :update do |page|
        page.replace_html :message,"&nbsp;<script type='text/javascript'>openAlert('#{@message}',200,60);</script>"
      end
    end
  end
  
  def delete_friend
    id = params[:id] || -1
    owner_id = session[:user_id] || -1
    friend=Friend.find_by_id_and_owner_id(id,owner_id)
    friend.destroy if friend
  end
  
end
